package com.lineage.server.timecontroller.server;

import com.lineage.server.datatables.lock.TownReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.gametime.L1GameTime;
import com.lineage.server.model.gametime.L1GameTimeAdapter;
import com.lineage.server.model.gametime.L1GameTimeClock;
import com.lineage.server.model.gametime.L1GameTimeListener;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerHomeTownTime {
  private static final Log _log = LogFactory.getLog(ServerHomeTownTime.class);
  
  private static ServerHomeTownTime _instance;
  
  private static L1TownFixedProcListener _listener;
  
  public static ServerHomeTownTime getInstance() {
    if (_instance == null)
      _instance = new ServerHomeTownTime(); 
    return _instance;
  }
  
  private ServerHomeTownTime() {
    startListener();
  }
  
  private void startListener() {
    if (_listener == null) {
      _listener = new L1TownFixedProcListener();
      L1GameTimeClock.getInstance().addListener((L1GameTimeListener)_listener);
    } 
  }
  
  private void fixedProc(L1GameTime time) {
    Calendar cal = time.getCalendar();
    int day = cal.get(5);
    if (day == 25) {
      monthlyProc();
    } else {
      dailyProc();
    } 
  }
  
  public void dailyProc() {
    _log.info("村庄系统：日处理启动");
    TownReading.get().updateTaxRate();
    TownReading.get().updateSalesMoneyYesterday();
    TownReading.get().load();
  }
  
  public void monthlyProc() {
    _log.info("村庄系统：月处理启动");
    World.get().setProcessingContributionTotal(true);
    Collection<L1PcInstance> players = World.get().getAllPlayers();
    Iterator<L1PcInstance> iter = players.iterator();
    while (iter.hasNext()) {
      L1PcInstance pc = iter.next();
      try {
        pc.save();
      } catch (Exception e) {
        _log.error(e.getLocalizedMessage(), e);
      } 
    } 
    int townId = 1;
    while (townId <= 10) {
      String leaderName = TownReading.get().totalContribution(townId);
      if (leaderName != null) {
        S_PacketBox packet = new S_PacketBox(23, leaderName);
        Iterator<L1PcInstance> iter2 = players.iterator();
        while (iter2.hasNext()) {
          L1PcInstance pc2 = iter2.next();
          if (pc2.getHomeTownId() == townId) {
            pc2.setContribution(0);
            pc2.sendPackets((ServerBasePacket)packet);
          } 
        } 
      } 
      townId++;
    } 
    TownReading.get().load();
    iter = players.iterator();
    while (iter.hasNext()) {
      L1PcInstance pc = iter.next();
      if (pc.getHomeTownId() == -1)
        pc.setHomeTownId(0); 
      pc.setContribution(0);
      try {
        pc.save();
      } catch (Exception e) {
        _log.error(e.getLocalizedMessage(), e);
      } 
    } 
    TownReading.get().clearHomeTownID();
    World.get().setProcessingContributionTotal(false);
  }
  
  private class L1TownFixedProcListener extends L1GameTimeAdapter {
    private L1TownFixedProcListener() {}
    
    public void onDayChanged(L1GameTime time) {
      ServerHomeTownTime.this.fixedProc(time);
    }
  }
}
